﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Drawing;
/// <summary>
/// Summary description for ImageCellMngr
/// </summary>
public class ImageCellMngr
{
    private static readonly int IMAGE_SIZE = 256;

    ArrayList m_CellList;
    ImageLoader m_pImgLoader;

    public ImageCellMngr()
    {
        m_CellList = null;
	    m_pImgLoader = new ImageLoader();
    }

    public void LoadMapImage(Transform pTransform)
    {
        m_CellList = new ArrayList();

	    int nLevel = pTransform.m_pProj.GetLevel();

	    Point tcLeftTop = new Point();
        Point tcRightBottom = new Point();
	    Point2D ptLeftTop = new Point2D();
        Point2D ptRightBottom = new Point2D();
        Point tcCenter = new Point();
        int nWidth, nHeight;
        Point2D ptVCenter = new Point2D();

	    nWidth	= pTransform.m_pProj.GetWidthView();
	    nHeight = pTransform.m_pProj.GetHeightView();

	    ptLeftTop = pTransform.m_pProj.PDisplay2RP(0, 0);	
	    tcLeftTop = pTransform.m_pProj.GetTileCoords(ptLeftTop);
	    ptRightBottom = pTransform.m_pProj.PDisplay2RP(nWidth-1, nHeight-1);	
	    tcRightBottom = pTransform.m_pProj.GetTileCoords(ptRightBottom);

	    tcCenter = pTransform.m_pProj.GetTileCoords(pTransform.m_pProj.GetCenterPoint());
	    ptVCenter = pTransform.m_pProj.RP2VP(pTransform.m_pProj.GetCenterPoint());	
	    ptVCenter.x -= (tcCenter.X * IMAGE_SIZE);
	    ptVCenter.y -= (tcCenter.Y * IMAGE_SIZE);

	    int nLeft, nTop, nRight, nBottom;	
	    int LeftPadding, TopPadding;
	    int nTileX, nTileY;	
	    int nMaxCols = (1 << nLevel);

	    string szURL;
	    //CFile f;
	    Rectangle rcView = new Rectangle();

	    nLeft = Math.Max(tcLeftTop.X, 0);
        nTop = Math.Max(tcLeftTop.Y, 0);
        nRight = Math.Min(tcRightBottom.X, nMaxCols);
        nBottom = Math.Min(tcRightBottom.Y, nMaxCols);

	    //Get Images
	    for (nTileX = nLeft; nTileX <= nRight; nTileX++)
	    {
		    for (nTileY = nTop; nTileY <= nBottom; nTileY++)
		    {
			    //nImages = ((signed __int64)nTileY * nMaxCols + nTileX) / 10000;
			    //CString strImage;
			    //strImage.Format(_T("%d_%d.png"), nTileY, nTileX);
			    //strFile.Format(_T("%s%d/%d/"), pTransform.m_strImagePath, nLevel, nImages);
			    //strFile += strImage;

                szURL = string.Format("http://222.255.236.178:808/ImageFeeder?Action=GetTiles&Level={0}&X={1}&Y={2}", nLevel, nTileX, nTileY);
    			

			    LeftPadding = (int) ((nTileX - tcCenter.X) * IMAGE_SIZE + (nWidth / 2) - ptVCenter.x);
			    TopPadding = (int) ((nTileY - tcCenter.Y) * IMAGE_SIZE + (nHeight / 2) - ptVCenter.y);

			    rcView.X = LeftPadding;
			    rcView.Y = TopPadding;
			    rcView.Width = IMAGE_SIZE;
			    rcView.Height = IMAGE_SIZE;

                m_pImgLoader.AsyncDownload(szURL, rcView, this);
		    }
	    }
    }

    public void SyncAdd(ImageCell pImageCell)
    {
	    m_CellList.Add(pImageCell);
    }

    public void Draw(Graphics g, Transform pTransform)
    {
        if (m_CellList != null)
        {
            for (int i = 0; i < m_CellList.Count; i++)
                ((ImageCell)m_CellList[i]).Draw(g);
        }
    }
}
